//副本
package InstanceZone

import (
	"sanguo/node/common/DropPool"
	battlelevelDef "sanguo/node/table/battlelevel"
	table_BattleLevel "sanguo/node/table/excel/table/BattleLevel"
	table_DropPool "sanguo/node/table/excel/table/DropPool"
	table_Monster "sanguo/node/table/excel/table/Monster"
	table_Resurgence "sanguo/node/table/excel/table/Resurgence"
	cs_msg "sanguo/protocol/cs/message"
	"strconv"
	"strings"

	"github.com/golang/protobuf/proto"
)

type InstanceZone struct {
	ID                  int32
	InitType            int32
	ActionPointCost     int32
	GoldCost            int32
	DiamondCost         int32
	ItemCost            map[int32]int32
	MonsterAward        map[int32]*MonsterAward //怪物奖励 key为怪物ID
	InstanceZoneAward   []*DropPool.Award       //关卡奖励
	OldMapID            int32
	OldPosX             int32
	OldPosY             int32
	OldPosZ             int32
	OldPosAngle         int32
	IsThroughOK         bool
	IsEnd               bool
	FreeResurgenceTimes int32
	ResurgenceTimes     int32
}

type MonsterAward struct {
	Count         int32
	ExpAward      int32
	GoldAward     int32
	MonsterAwards map[int32]*DropPool.Award
}

//计算关卡奖励
func InitInstanceZoneAward(DropPoolID int32) []*DropPool.Award {
	awards := []*DropPool.Award{}
	DropDef := table_DropPool.Table_.IDMap[DropPoolID]
	if DropDef != nil {
		awards = append(awards, DropPool.InitAward(DropPoolID))
	}
	return awards
}

//计算怪物奖励
func InitMonsterAward(StageConfig string) map[int32]*MonsterAward {
	monsterAward := make(map[int32]*MonsterAward)

	s := strings.Split(StageConfig, "_")
	if len(s) != 2 {
		return monsterAward
	}

	xmlID, err := strconv.Atoi(s[1])
	if err != nil {
		return monsterAward
	}
	//读表
	monsterDef := battlelevelDef.GetDef(int32(xmlID))
	if monsterDef == nil {
		return monsterAward
	}

	for _, monster := range monsterDef.MonsterList.Monsters {
		monsterID := monster.MonsterId
		count := monster.MonsterCount

		MonsterDef := table_Monster.Table_.IDMap[monsterID]
		if MonsterDef == nil {
			continue
		}

		mA := &MonsterAward{
			Count:         count,
			ExpAward:      MonsterDef.Exp * count,
			GoldAward:     MonsterDef.Gold * count,
			MonsterAwards: map[int32]*DropPool.Award{},
		}

		DropDef := table_DropPool.Table_.IDMap[MonsterDef.DropPool]
		if DropDef != nil {
			for i := 1; i <= int(count); i++ {
				mA.MonsterAwards[int32(i)] = DropPool.InitAward(MonsterDef.DropPool) //掉落库
			}
		}
		monsterAward[monsterID] = mA
	}

	return monsterAward
}

func InitInstanceZone(ID, InitType int32, mapID, x, y, z, angle int32) *InstanceZone {
	IZDef := table_BattleLevel.Table_.IDMap[ID]

	if IZDef == nil {
		return nil
	}
	instanceZone := &InstanceZone{
		ID:                ID,
		InitType:          InitType,
		ItemCost:          map[int32]int32{},
		MonsterAward:      InitMonsterAward(IZDef.StageConfig),
		InstanceZoneAward: InitInstanceZoneAward(IZDef.DropPool),
		OldMapID:          mapID,
		OldPosX:           x,
		OldPosY:           y,
		OldPosZ:           z,
		OldPosAngle:       angle,
		IsThroughOK:       false,
		IsEnd:             false,
		ResurgenceTimes:   int32(0),
	}

	RegDef := table_Resurgence.Table_.IDMap[IZDef.ResurgenceId]
	if RegDef == nil {
		instanceZone.FreeResurgenceTimes = 0
	} else {
		instanceZone.FreeResurgenceTimes = RegDef.ResurgenceTimes
	}

	return instanceZone
}

//增加奖励
func (this *InstanceZone) DoubleAward(DropPoolID, power int32) {
	if power == 0 {
		return
	}
	DropDef := table_DropPool.Table_.IDMap[DropPoolID]
	if DropDef == nil {
		return
	}

	for i := int32(0); i < power; i++ {
		this.InstanceZoneAward = append(this.InstanceZoneAward, DropPool.InitAward(DropPoolID))
	}
}

func (this *InstanceZone) GetInstanceZoneId() int32 {
	return this.ID
}

//复活
func (this *InstanceZone) Resurgence() (int32, int32) {

	if this.FreeResurgenceTimes > 0 {
		this.FreeResurgenceTimes--
	} else {
		this.ResurgenceTimes++
	}
	return this.FreeResurgenceTimes, this.ResurgenceTimes
}

//打包关卡奖励
func (this *InstanceZone) PackInstanceZoneAward() *cs_msg.InstanceZoneAward {
	ret := &cs_msg.InstanceZoneAward{}
	for _, a := range this.InstanceZoneAward {
		if a != nil {
			award := DropPool.PackAward(a)
			ret.Awards = append(ret.Awards, award)
		}
	}

	return ret
}

//打包怪物奖励
func (this *InstanceZone) PackMonsterAward() []*cs_msg.InstanceZoneMonster {
	ret := []*cs_msg.InstanceZoneMonster{}
	for monsterID, v := range this.MonsterAward {
		IZMonster := &cs_msg.InstanceZoneMonster{
			MonsterID: proto.Int32(monsterID),
			Count:     proto.Int32(v.Count),
			Exp:       proto.Int32(v.ExpAward),
			Gold:      proto.Int32(v.GoldAward),
		}
		if len(v.MonsterAwards) != 0 {
			for i, a := range v.MonsterAwards {
				if a != nil {
					monsterAward := &cs_msg.MonsterAward{
						Index: proto.Int32(i),
						Award: DropPool.PackAward(a),
					}
					IZMonster.MonsterAwards = append(IZMonster.MonsterAwards, monsterAward)
				}
			}
		}
		ret = append(ret, IZMonster)
	}
	return ret
}

//打包所有奖励
func (this *InstanceZone) PackAllAward() *cs_msg.EnterInstanceZoneToC {
	ret := &cs_msg.EnterInstanceZoneToC{
		InstanceZoneID:       proto.Int32(this.ID),
		InstanceZoneAward:    this.PackInstanceZoneAward(),
		InstanceZoneMonsters: this.PackMonsterAward(),
	}
	return ret
}

// 返回所有奖励
func (this *InstanceZone) GetAllAward() (int32, int32, []*DropPool.Award) {
	exp := int32(0)
	gold := int32(0)
	awards := []*DropPool.Award{}
	for _, award := range this.InstanceZoneAward {
		awards = append(awards, award)
	}
	for _, m := range this.MonsterAward {
		exp += m.ExpAward
		gold += m.GoldAward
		for _, award := range m.MonsterAwards {
			awards = append(awards, award)
		}
	}

	return exp, gold, awards
}
